package com.inapp.ocreader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.itwizard.mezzofanti.OCR;

public class InstallActivity extends Activity 
{
	private static final String TAG = "InstallActivity.java";
	
	private ProgressBar m_LocalProgressBar = null;			// parent progress bar, to indicate the install status
	private static Handler m_ParentMessageHandler = null;	// parent message handler, for communication
	private AssetsManager m_AssetsManager = null;			// local AssetsManager, to install the language assets from the package

	public static void SetParentMessageHandler(Handler mh)
	{
		m_ParentMessageHandler = mh;
	}
	
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        try
        {
	        Log.v(TAG, "Starting app ---------------------------");
	        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	        
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	                WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        
	        Window window = getWindow();
	        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		        
	        setContentView(R.layout.install);
 
            m_LocalProgressBar = (ProgressBar) findViewById(R.id.installactivity_progressbar);
            m_AssetsManager = new AssetsManager(m_LocalMessageHandler, m_LocalProgressBar);
            m_AssetsManager.InstallLanguageAssetsJob();
        }
        catch (Exception ex)
        {
        	Log.e(TAG, "mexeption: " + ex.toString());
        }
        catch (Throwable t)
        {
        	Log.e(TAG, "mexpcetion: " + t.toString());
        }
    }
	
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
	{		
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if ( keyCode == KeyEvent.KEYCODE_BACK )
        { 	
            if (event.getRepeatCount() == 0)
            {
            	if (AssetsManager.IsInstalled() == false)
            	{
            		SendKillToParent();
            	}
            }  
        }
        
        return super.onKeyDown(keyCode, event);
    }

	private Handler m_LocalMessageHandler = new Handler() 
	{
		@Override
		public void handleMessage(Message msg) 
		{
			switch(msg.what)
			{
				case R.id.assetsmanager_installingdone:
				{
	            	Log.v(TAG, "Installing done.");
					Bundle bdl = msg.getData();
					boolean bModifOnDisk = bdl.getBoolean("modifondisk");
					if (bModifOnDisk)
						OCR.mConfig.LoadFabricDefaults();
					
	    			finish();
					break;
				}	
				case R.id.assetsmanager_installingcorrupted:
				{
					Bundle bdl = msg.getData();
					long lNeededSpace = bdl.getLong("needed_space");
					long lFreeSpace = bdl.getLong("free_space");
					DisplayProblemsInstallingAlert(lNeededSpace, lFreeSpace);
		    	    break;
				}
				default:
					break;
					
			}
			
			super.handleMessage(msg);
		}
	};
	
	private void DisplayProblemsInstallingAlert(long lNeededSpace, long lFreeSpace)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    builder.setIcon(android.R.drawable.ic_dialog_alert);
	    builder.setTitle(R.string.assetsmanager_title);
	    builder.setMessage(getString(R.string.assetsmanager_msg1) + (lNeededSpace/(1024*1024)) + "[MB] " 
	    		+ getString(R.string.assetsmanager_msg2)+ (lFreeSpace/(1024*1024)) + "[MB]");
	    builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
	    {
			//@Override
			public void onClick(DialogInterface dialog, int which) 
			{
				SendKillToParent();
				finish();
			}
	    	
	    });
	    builder.setCancelable(false);
	    builder.show();
	    
	}
	
	private void SendKillToParent()
	{
		Log.v(TAG, "Send kill to parent");
		m_ParentMessageHandler.sendEmptyMessageDelayed(R.id.installactivity_killApp, 500);
	}

}
